from django.shortcuts import render,redirect,HttpResponse
from user_system import models
from utils.auth_manog import auth_manog_done
from utils import pagination
import re


# Create your views here.
@auth_manog_done
def gameserver_group_list(request):
    if request.method == "GET":
        project_id = request.session.get("project_id")
        if not project_id:
            return HttpResponse("没有选择项目，不可操作")
        gameserver_group_lists = models.GameServerGroup.objects.filter(b_project_id=project_id).all()
        gameserver_group_num = gameserver_group_lists.count()
        current_page = int(request.GET.get("Page", 1))  # 获取用户get请求的页码数
        select_current_page = int(request.COOKIES.get("per_page_count", 13))  # 获取当前页面cookie记录的显示条数,默认是10
        page_obj = pagination.Page(None,None,current_page, len(gameserver_group_lists),
                                   select_current_page)  # 创建page_obj对象  per_page_count=10,page_num=7是变量，可改动
        gameserver_group_lists = gameserver_group_lists[page_obj.start_count:page_obj.end_count]  # 每页显示数据条数列表
        page_list = page_obj.page_str("")  # 分页的所有内容
        return render(request, "super_cmdb/gameserver_group_list.html",{
            "gameserver_group_lists": gameserver_group_lists,
            "page_list": page_list,
            "gameserver_group_num": gameserver_group_num
        })

@auth_manog_done
def gameserver_group_add(request):
    project_id = request.session.get("project_id")
    if request.method == "GET":
        return render(request,"super_cmdb/gameserver_group_add.html")
    if request.method == "POST":
        rsg = {'status': 0, 'message': None, 'error': None}
        group_name = request.POST.get("group_name")
        if not models.GameServerGroup.objects.filter(group_name=group_name,b_project_id=project_id).first():
            try:
                models.GameServerGroup.objects.create(group_name=group_name,b_project_id=project_id)
                rsg["message"] = "分区添加成功"
            except Exception as e:
                rsg["status"] = 2
                rsg["message"] = e
        else:
            rsg["status"] = 3
            rsg["message"] = "该分区名称已存在"
        return HttpResponse(rsg["message"])

@auth_manog_done
def gameserver_group_edit(request,gameserver_group_id):
    """编辑分区"""
    if request.method == "GET":
        gameserver_group_info = models.GameServerGroup.objects.filter(id=gameserver_group_id).first()
        return render(request,"super_cmdb/gameserver_group_edit.html",{"gameserver_group_info":gameserver_group_info})
    if request.method == "POST":
        rsg = {'status': 0, 'message': None, 'error': None}
        group_name = request.POST.get("group_name")
        try:
            models.GameServerGroup.objects.filter(id=gameserver_group_id).update(group_name=group_name)
            rsg["message"] = "分区修改成功"
        except Exception as e:
            rsg["status"] = 2
            rsg["message"] = e
        return HttpResponse(rsg["message"])


@auth_manog_done
def gameserver_group_del(request):
    if request.method == "POST":
        gameserver_group_id = request.POST.get("gameserver_group_id")   #前端传过来的全是字符串
        nums = re.findall(r'\d+', gameserver_group_id)      #匹配user_id组成数组
        if isinstance(nums,list):    #判断user_id是不是一个list
            for id in nums:
                #id = int(id)
                group_name = models.GameServerGroup.objects.filter(id=id).first().group_name
                models.GameServerGroup.objects.filter(id=id).delete()
                log_success = "删除分区【%s】成功" % group_name
                print(log_success)
        return redirect("/super_cmdb/gameserver_group_list/")
